package loja.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import loja.model.despesa.Despesa;
import loja.services.factory.ServiceFactory;
import loja.services.factory.helper.ServiceFactoryHelper;

@FacesConverter(forClass = Despesa.class)
public class DespesaConverter implements Converter {

	private ServiceFactory serviceFactory = ServiceFactoryHelper
			.getServiceFactory();

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1,
			String stringDespesa) {

		if (stringDespesa == null)
			return null;

		if (stringDespesa.trim().length() == 0)
			return null;

		Long idDespesa = Long.valueOf(stringDespesa);
		return this.serviceFactory.getDespesaService()
				.consultarDespesaPorCodigo(idDespesa);

	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1,
			Object objDespesa) {
		if (objDespesa == null)
			return null;
		Despesa despesa = (Despesa) objDespesa;
		return String.valueOf(despesa.getIdDespesa());
	}

}
